package com.example.trueorfalse.tools;

import android.content.Context;
import android.content.Intent;

import com.example.trueorfalse.R;
import com.example.trueorfalse.model.Question;

//对Question进行查改操作
public class QuestionLab {

    private static Question[] sQuestions;
    private static QuestionLab sQuestionLab;

    //该类的入口，外部类都调用该方法获取到QuestionLab对象
    public static QuestionLab get(){
        if(sQuestionLab == null){
            sQuestionLab = new QuestionLab();
        }
        return sQuestionLab;
    }
    //无参构造函数
    public QuestionLab() {
        //对问题初始化
        if(sQuestions==null) {
            createQuestions();
        }
    }

    public Question[] getQuestions() {
        if(sQuestions==null){
            createQuestions();
        }
        return sQuestions;
    }

    public void setQuestions(Question[] sQuestions) {
        QuestionLab.sQuestions = sQuestions;
    }

    public void createQuestions(){
        Question[] q = {
                new Question(R.string.q1,""),
                new Question(R.string.q2,""),
                new Question(R.string.q3,""),
                new Question(R.string.q4,""),
                new Question(R.string.q5,""),
                new Question(R.string.q6,""),
                new Question(R.string.q7,""),
                new Question(R.string.q8,""),
                new Question(R.string.q9,""),
                new Question(R.string.q10,""),
        };
        sQuestions = q;
    }


    public void sendMessage(Context context){
        //1、创建StringBuffer拼接字符串
        StringBuffer sbf = new StringBuffer();
        //遍历拼接
        for(Question question:sQuestions){
            String str = context.getString(question.getTextResId()) + "->" + question.getAnswer();
            sbf.append(str);
        }
        //使用隐式intent调起QQ
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, sbf.toString());
        sendIntent.setType("text/plain");
        try {
            //setClassName跳转到不同应用下的Activity
            sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
            if (chooserIntent == null) {
                return;
            }
            context.startActivity(chooserIntent);
        } catch (Exception e) {
            context.startActivity(sendIntent);
        }
    }


}
